﻿var currentOrgId = null;
var parentOrgId = null;
var treeInstance = null;
$(function () {
    //树初始化
    treeInstance = $('#treeDiv').tree({//tree为容器ID
        asyn: true,
        rootId: 'root',
        rootText: '组织机构',
        CommandName: 'java.tree.command',
        onNodeClick: leftClick,
        onLoaded: doOnLoaded,
        params: {
            sqlId: 'organization.firstFloor.tree',
            childSqlId: 'organization.getChildren',
            parentId: 'root'
        },
        contextMenu: function (record) {
            var items = [];

            if (record.id != 'root' && record.parentId != "root") {
                if (enableCreateOrg) {
                    items.push({ text: "注册子组织", icon: Global.contextPath + "/Images/icon_new.gif", id: record.id, action: addAction, orgName: record.text, parent_id: record.parentId });
                }

                if (enableModifyOrg) {
                    items.push({ text: "修改组织", icon: Global.contextPath + "/Images/ico_edit.gif", id: record.id, action: updateAction, orgName: record.text, parent_id: record.parentId });
                }

                if (enableDeleteOrg) {
                    items.push({ text: "删除组织", icon: Global.contextPath + "/Images/ico_del.gif", id: record.id, action: deleteAction, orgName: record.text, parent_id: record.parentId });
                }
            } else if (record.parentId == "root") {
                if (enableModifyOrg) {
                    items.push({ text: "修改组织", icon: Global.contextPath + "/Images/ico_edit.gif", id: record.id, action: updateAction, orgName: record.text, parent_id: record.parentId });
                }
                if (enableCreateOrg) {
                    items.push({ text: "注册子组织", icon: Global.contextPath + "/Images/icon_new.gif", id: record.id, action: addAction, orgName: record.text, parent_id: record.parentId });
                }
            } /*else {
                items.push({ text: "注册子组织", icon: Global.contextPath + "/Images/icon_new.gif", id: "", action: addAction, orgName: "根组织", parent_id: record.parentId });
            }*/

            return {
                width: 100,
                items: items
            };
        }
    });
    $('#treeDiv').tree().expandLevel(null, 2);
});

function reloadTree(){
    treeInstance.reload();
    treeInstance.expandLevel(null, 2);
    document.getElementById("content_frame").src = document.getElementById("content_frame").src;
}

function leftClick(id, text, record, currentNode) {
    currentOrgId = id;
    var url = Global.contextPath + '/Modules/SecurityConsole/Organization/OrganizationList.aspx';
    if (id) {//ID不存在，点击根节点
        if (id == 'root') {
            id = '';
            text = '根组织';
        }
        url += '?Parent=' + id + '&ParentName=' + encodeURI(text);
    }

    document.getElementById("content_frame").src = url;
}

function doOnLoaded() {
    if (document.getElementById("content_frame").src == "") {
        var url = Global.contextPath + '/Modules/SecurityConsole/Organization/OrganizationList.aspx';
        document.getElementById("content_frame").src = url;
    }
}

function addAction() {
    var url = "OrganizationEdit.aspx";
    var params = { FormViewMode: "Insert" };
    params.title = "操作注册";
    params.Parent = this.data.id;
    params.ParentName = this.data.orgName;

    showCenterModalDialog(buildUrl(url, params), 500, 175, null);
    reloadTree();
}

function updateAction() {
    var url = "OrganizationEdit.aspx";
    var params = { FormViewMode: "Edit" };
    params.title = "操作修改";
    params.Id = this.data.id;

    showCenterModalDialog(buildUrl(url, params), 500, 175, null);
    reloadTree();
}

function deleteAction() {
    var orgId = this.data.id;

    if (window.confirm("确认要删除组织机构吗？")) {
        $.dataservice("OrganizationService$DeleteOrganization", { orgId: orgId }, function (response) {
            reloadTree();
        });
    }
}

